package com.luomo.wenyuan.modules.wenyuan.controller;

import com.luomo.wenyuan.common.utils.PageUtils;
import com.luomo.wenyuan.common.utils.R;
import com.luomo.wenyuan.modules.wenyuan.entity.TReaderEntity;
import com.luomo.wenyuan.modules.wenyuan.service.TReaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * @author
 * @email
 * @date 2020-10-19 15:58:40
 */
@RestController
@RequestMapping("generator/treader")
public class TReaderController {
    @Autowired
    private TReaderService tReaderService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = tReaderService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        TReaderEntity tReader = tReaderService.getById(id);
        return R.ok().put("tReader", tReader);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @Transactional(rollbackFor = Exception.class)
    public R save(@RequestBody TReaderEntity tReader) {
        tReader.setId(tReaderService.getNextId());
        tReader.setRegistryDate(new Date());
        tReaderService.save(tReader);
        return R.ok().put("data", tReader.getId());
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody TReaderEntity tReader) {
        tReaderService.updateById(tReader);

        return R.ok();
    }

    /**
     * 注销
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] ids) {
        for (String id : ids) {
            TReaderEntity readerEntity = tReaderService.getById(id);
            readerEntity.setCancelDate(new Date());
            tReaderService.updateById(readerEntity);
        }
        return R.ok();
    }

}
